package com.bjypc.youth.web.json;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.MessageSource;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class ImportBasicDataAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -6508572308233342943L;

	private Logger logger = Logger.getLogger(ImportBasicDataAction.class);
	private MessageSource messageSource;
	private String msg;
	/**
	 * status field to indicate whether operation succeeds
	 */
	private boolean success;
	private boolean failure;

	@Override
	public String execute() throws Exception {
		// basic datas in deployed server path
		String basicdataDir = ServletActionContext.getServletContext()
				.getRealPath("/basicdata/");
		// basic data in disk
		String dataPath = messageSource.getMessage("BasicDataPath", null,
				"c:\\temp\\basicdata\\", null);
		// get seperate path
		String chBasicData = dataPath + File.separator + "CH" + File.separator
				+ "index.htm";
		String enBasicData = dataPath + File.separator + "EN" + File.separator
				+ "index.htm";
		String excelBasicData = dataPath + File.separator + "Excel"
				+ File.separator;

		// check
		logger.info("basicdata in Web Server directory:" + basicdataDir);
		logger.info("basicdata save directory:" + dataPath);
		logger.info("Chinese index:" + chBasicData);
		logger.info("Chinese index:" + enBasicData);
		logger.info("Excel directory:" + excelBasicData);
		logger.info("dataPath directory:" + dataPath);

		// check if files exists
		File chBasicDataFile = new File(chBasicData);
		File enBasicDataFile = new File(enBasicData);
		File excelBasicDataDir = new File(excelBasicData);
		boolean filesExists = false;
		if (!(filesExists = chBasicDataFile.exists())) {
			filesExists = true;
			logger
					.error("Chinese Basic data files does not exists!please put it into the directory");
		} else {
			logger.info("Chinese Basic data files exists in "
					+ chBasicDataFile.getAbsolutePath());
		}

		if (!(filesExists = enBasicDataFile.exists())) {
			filesExists = true;
			logger
					.error("English Basic data files does not exists!please put it into the directory");
		} else {
			logger.info("English Basic data files exists in "
					+ enBasicDataFile.getAbsolutePath());
		}

		if (!(filesExists = excelBasicDataDir.exists())) {
			logger
					.error("Excel Basic data files does not exists!please put it into the directory");
		} else {
			logger.info("Excel Basic data files exists in "
					+ excelBasicDataDir.getAbsolutePath());
		}
		if(filesExists){
			FileUtils.copyDirectory(new File(dataPath), new File(basicdataDir+File.separator));
			this.success=true;
			this.msg="数据已经全部复制到服务器中，请点击链接查看！";
		}else{
			this.success=false;
			this.failure=true;
			this.msg="数据已经存在！";
		}
		
		
		return SUCCESS;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public boolean isFailure() {
		return failure;
	}

	public void setFailure(boolean failure) {
		this.failure = failure;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}



	@JSON(serialize = false, deserialize = false)
	public MessageSource getMessageSource() {
		return messageSource;
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

}
